package edu.haiu.training.data1.content;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Content {
	private int id_content;
	private String name_content;
	private Date date_update;
	private String detail_content;
	private static final String username1 ="root";
	private static final String password1 ="123456";
	private static final String database1 ="jdbc:mysql://localhost:3306/mydb";
	public int getId_content() {
		return id_content;
	}
	public void setId_content(int id_content) {
		this.id_content = id_content;
	}
	public String getName_content() {
		return name_content;
	}
	public void setName_content(String name_content) {
		this.name_content = name_content;
	}
	public Date getDate_update() {
		return date_update;
	}
	public void setDate_update(String date) throws ParseException {
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		this.date_update = (Date)formatter.parse(date);	
	}
	public String getDetail_content() {
		return detail_content;
	}
	public void setDetail_content(String detail_content) {
		this.detail_content = detail_content;
	}
	public String add() throws Exception{
		Connection connection = DriverManager.getConnection(database1,username1,password1);
		String query = "INSERT INTO mydb.content(`name_content`, `date_update`, `detail_content`) VALUES(?,?,?)";
		PreparedStatement ps= connection.prepareStatement(query);
		ps.setString(1, name_content);
		ps.setDate(2, new java.sql.Date(date_update.getTime()));
		ps.setString(3, detail_content);
		ps.executeUpdate();
		connection.close();
		return "success";
	}
	
	public void deleteContent(int idContent) throws Exception{
		Connection connection = DriverManager.getConnection(database1,username1,password1);
		String qs = "DELETE FROM mydb.content where id_content=?";
		PreparedStatement ps = connection.prepareStatement(qs);
		ps.setInt(1, idContent);
		ps.executeUpdate();
	}
	
}
